﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CnSharp.CodeGenerator;
using CnSharp.IO;

namespace CnSharp.SSMS.Demo.CodeGen
{
    public class Util
    {
        private static IGenerator generator;

        public static IGenerator CodeGenerator
        {
            get { return generator ?? (generator = new XsltGenerator()); }
        }

        public static string GetSystemCodeTemplateDir(string group)
        {
            var dir = string.Format("{0}Template\\{1}\\", Common.AppRoot, group);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return dir;
        }

        public static string GetCustomCodeTemplateDir(string group)
        {
            var oldDir = string.Format(
                "{0}\\{1}\\Template\\",
                Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                Application.ProductName);
            var newDir = string.Format(
                "{0}\\{1}\\Templates\\",
                Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                Assembly.GetExecutingAssembly().GetName().Name);
            if (!Directory.Exists(newDir) && Directory.Exists(oldDir))
            {
                FileIO.CopyFiles(oldDir, newDir);
            }
            var dir = Path.Combine(newDir, group);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return dir;
        }

    }
}